Java.perform(function() {  
    // 1. OkHttp3 拦截
    try {
        var OkHttpClient = Java.use('okhttp3.OkHttpClient');
        var RealCall = Java.use('okhttp3.RealCall');
        var Request = Java.use('okhttp3.Request');
        var RequestBody = Java.use('okhttp3.RequestBody');
        var MediaType = Java.use('okhttp3.MediaType');

        // 拦截 RealCall.execute()
        RealCall.execute.implementation = function() {
            console.log('\n[+] Intercepted OkHttp Request');
            var request = this.originalRequest();
            logRequest(request);
            var response = this.execute();
            logResponse(response);
            return response;
        };

        // 拦截 RealCall.enqueue()
        RealCall.enqueue.implementation = function(callback) {
            console.log('\n[+] Intercepted OkHttp Async Request');
            var request = this.originalRequest();
            logRequest(request);
            return this.enqueue(callback);
        };

    } catch(err) {
        console.log('OkHttp hook error:', err);
    }

    // 2. HttpURLConnection 拦截
    try {
        var HttpURLConnection = Java.use('java.net.HttpURLConnection');
        HttpURLConnection.connect.implementation = function() {
            console.log('\n[+] Intercepted HttpURLConnection');
            console.log('URL:', this.getURL().toString());
            console.log('Method:', this.getRequestMethod());

            // 打印请求头
            var headers = {};
            var headerFields = this.getRequestProperties();
            var it = headerFields.keySet().iterator();
            while(it.hasNext()) {
                var key = it.next();
                var value = headerFields.get(key);
                headers[key] = value.toString();
            }
            console.log('Headers:', JSON.stringify(headers, null, 2));

            return this.connect();
        };
    } catch(err) {
        console.log('HttpURLConnection hook error:', err);
    }

    // 3. 常见加密方法拦截
    try {
        // 拦截 javax.crypto.Cipher
        var Cipher = Java.use('javax.crypto.Cipher');
        Cipher.doFinal.overload('[B').implementation = function(data) {
            console.log('\n[+] Cipher.doFinal called');
            console.log('Algorithm:', this.getAlgorithm());
            console.log('Input:', bytesToString(data));
            var result = this.doFinal(data);
            console.log('Output:', bytesToString(result));
            return result;
        };

        // 拦截 MessageDigest (常用于哈希)
        var MessageDigest = Java.use('java.security.MessageDigest');
        MessageDigest.digest.overload().implementation = function() {
            console.log('\n[+] MessageDigest.digest called');
            console.log('Algorithm:', this.getAlgorithm());
            var result = this.digest();
            console.log('Result:', bytesToString(result));
            return result;
        };
    } catch(err) {
        console.log('Crypto hook error:', err);
    }

    // 辅助函数
    function logRequest(request) {
        console.log('URL:', request.url().toString());
        console.log('Method:', request.method());
        console.log('Headers:', JSON.stringify(request.headers().toString(), null, 2));

        var body = request.body();
        if(body) {
            try {
                var buffer = Java.use('okio.Buffer').$new();
                body.writeTo(buffer);
                console.log('Body:', buffer.readUtf8());
            } catch(err) {
                console.log('Error reading body:', err);
            }
        }
    }

    function logResponse(response) {
        console.log('\n[+] Response');
        console.log('Code:', response.code());
        console.log('Headers:', JSON.stringify(response.headers().toString(), null, 2));

        try {
            var body = response.body();
            if(body) {
                console.log('Body:', body.string());
            }
        } catch(err) {
            console.log('Error reading response body:', err);
        }
    }

    function bytesToString(bytes) {
        if(!bytes) return null;
        try {
            var result = [];
            for(var i = 0; i < bytes.length; i++) {
                result.push(bytes[i]);
            }
            return 'Bytes[' + result.join(', ') + ']';
        } catch(err) {
            return '<Error converting bytes>';
        }
    }

    // 4. 常见自定义拦截器的 Hook
    try {
        // Hook所有实现了Interceptor接口的类
        Java.enumerateLoadedClasses({
            onMatch: function(className) {
                if(className.toLowerCase().includes('interceptor')) {
                    try {
                        var InterceptorClass = Java.use(className);
                        if(InterceptorClass.intercept) {
                            console.log('[+] Found Interceptor:', className);
                            InterceptorClass.intercept.implementation = function(chain) {
                                console.log('\n[+] Custom Interceptor:', className);
                                var request = chain.request();
                                logRequest(request);
                                var response = this.intercept(chain);
                                logResponse(response);
                                return response;
                            };
                        }
                    } catch(err) {}
                }
            },
            onComplete: function() {}
        });
    } catch(err) {
        console.log('Custom interceptor hook error:', err);
    }
});